package com.example.reactor;

import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

@Slf4j
public class Practice4 {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        Mono.just(list)
                .flatMap(
                        new Function<List<Integer>, Mono<? extends List<Integer>>>() {
                            @Override
                            public Mono<? extends List<Integer>> apply(List<Integer> integerList) {
                                log.info(integerList.toString());
                                return Mono.just(integerList);
                            }
                        }
                ).subscribe();

        System.out.println("---------------------------------------分割线-------------------------------------");

        Flux.fromIterable(list)
                .flatMap(
                        new Function<Integer, Publisher<? extends Integer>>() {
                            @Override
                            public Publisher<? extends Integer> apply(Integer integer) {
                                log.info(integer.toString());
                                return Mono.just(integer);
                            }
                        }
                ).subscribe();

        System.out.println("---------------------------------------分割线-------------------------------------");

        Flux.fromIterable(list)
                .collectList()
                .map(
                        new Function<List<Integer>, List<Integer>>() {
                            @Override
                            public List<Integer> apply(List<Integer> integers) {
                                log.info(integers.toString());
                                return integers;
                            }
                        }
                ).subscribe();


        System.out.println("---------------------------------------分割线-------------------------------------");


        // Mono转Flux
        Mono.just("Hello")
                .flatMapMany(new Function<String, Publisher<? extends String>>() {
                    @Override
                    public Publisher<? extends String> apply(String i) {
                        log.info(i);
                        return Mono.just(i);
                    }
                })
                .subscribe();

        System.out.println("---------------------------------------分割线-------------------------------------");

        // Flux转Mono
        Flux.just("Hello", "World")
                .collectList()
                .map(new Function<List<String>, String>() {
                    @Override
                    public String apply(List<String> s) {
                        log.info(s.toString());
                        return s.get(0);
                    }
                })
                .subscribe(
                        new Consumer<String>() {
                            @Override
                            public void accept(String s) {
                                log.info(s);
                                log.info("执行完成");
                            }
                        }
                );

        System.out.println("---------------------------------------分割线-------------------------------------");

        Mono.just(list)
                .flatMapMany(Flux::fromIterable)
                .flatMap(i -> {
                    log.info(i.toString());
                    return Mono.just(i * i);
                })
                .subscribe(
                        i -> log.info(String.valueOf(i))
                );

    }
}
